package com.neusoft.controller;

import com.neusoft.po.BusinessAdmin;
import com.neusoft.po.CommonResult;
import com.neusoft.service.BusinessAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RefreshScope //开启动态刷新
@RestController
@RequestMapping("/BusinessAdminController")
public class BusinessAdminController {

    @Autowired
    private BusinessAdminService businessAdminService;

//    @RequestMapping("/listBusiness")
//    public List<BusinessAdmin> listBusiness(@RequestParam(value = "businessName", required = false) String businessName,
//                                       @RequestParam(value = "businessAddress", required = false) String businessAddress) {
//        return businessAdminService.listBusiness(businessName, businessAddress);
//    }
@GetMapping("/listBusiness/{businessName}/{businessAddress}")
public CommonResult<List> listBusiness(@PathVariable("businessName") String businessName,
                                                      @PathVariable("businessAddress") String businessAddress) {
    List<BusinessAdmin> businessAdmins = businessAdminService.listBusiness(businessName, businessAddress);
    return new CommonResult<>(200, "success", businessAdmins);
}

    /*@PostMapping("/saveBusiness")
    public CommonResult<Integer> saveBusiness(@RequestBody BusinessAdmin business) throws Exception {
        int result = businessAdminService.saveBusiness(business);
        return new CommonResult<>(200, "success", result);
    }*/

    @PostMapping("/saveBusiness/{businessId}/{password}/{businessName}")
    public CommonResult<Integer> saveUser(@PathVariable("businessId") String userId,
                                          @PathVariable("password") String password,
                                          @PathVariable("businessName") String userName) throws Exception {
        BusinessAdmin param = new BusinessAdmin();
        param.setBusinessId(Integer.parseInt(userId));
        param.setPassword(password);
        param.setBusinessName(userName);
        int result = businessAdminService.saveBusiness(param);
        return new CommonResult(200, "success", result);
    }

    @GetMapping("/getById/{businessId}")
    public CommonResult<Integer> getById(@PathVariable("businessId") String businessId) throws Exception {
        int result = businessAdminService.getById(businessId);
        return new CommonResult<>(200, "success", result);
    }

    @DeleteMapping("/remove/{businessId}")
    public CommonResult<Integer> removeBusiness(@PathVariable("businessId") int businessId) {
        int result = businessAdminService.removeBusiness(businessId);
        return new CommonResult<>(200, "success", result);
    }

    @GetMapping("/getBusinessByIdByPass/{businessId}/{password}")
    public CommonResult<BusinessAdmin> getBusinessByIdByPass(@PathVariable("businessId") Integer businessId,
                                                             @PathVariable("password") String password) {
        BusinessAdmin businessAdmin = businessAdminService.getBusinessByIdByPass(businessId, password);
        return new CommonResult<>(200, "success", businessAdmin);
    }

    @GetMapping("/getBusinessById/{businessId}")
    public CommonResult<BusinessAdmin> getBusinessById(@PathVariable("businessId") Integer businessId) {
        BusinessAdmin businessAdmin = businessAdminService.getBusinessById(businessId);
        return new CommonResult<>(200, "success", businessAdmin);
    }

    @PostMapping("/updateBusiness")
    public CommonResult updateBusiness(@ModelAttribute BusinessAdmin updateDto) {
        int result = businessAdminService.updateBusiness(
                updateDto.getBusinessId(),
                updateDto.getPassword(),
                updateDto.getBusinessName(),
                updateDto.getBusinessAddress(),
                updateDto.getBusinessExplain(),
                updateDto.getStarPrice(),
                updateDto.getDeliveryPrice(),
                updateDto.getBusinessImg()
        );
        return new CommonResult(200, "success", result);
    }

    @PostMapping("/updateBusinessByPassword/{businessId}/{password}")
    public CommonResult<Integer> updateBusinessByPassword(@PathVariable("businessId") Integer businessId,
                                                          @PathVariable("password") String password) {
        int result = businessAdminService.updateBusinessByPassword(businessId, password);
        return new CommonResult<>(200, "success", result);
    }
}